home *** CD-ROM | disk | FTP | other *** search
/ Aminet 7 / Aminet 7 - August 1995.iso / Aminet / docs / misc / ConcNews.lha / news / amiga.programming / comp.sys.amiga.programmer_3951_000021.msg < prev    next >
Encoding:
Text File  |  1994-11-27  |  3.6 KB  |  166 lines

  1. Path: dd.chalmers.se!news.chalmers.se!sunic!pipex!uunet!hearst.acc.Virginia.EDU!adastra!mbs
  2. Newsgroups: comp.sys.amiga.programmer
  3. From: mbs@adastra.cvl.va.us (Michael B. Smith)
  4. Subject: Re: Get WindowPtr of CLI-Windows?
  5. Distribution: world
  6. References:  <1E25B549H000493E2H@newton.han.de>
  7. X-NewsSoftware: GRn 2.0j Jan 8, 1994
  8. MIME-Version: 1.0
  9. Content-Type: text/plain; charset=iso-8859-1
  10. Content-Transfer-Encoding: 8bit
  11. Message-ID: <mbs.2naj@adastra.cvl.va.us>
  12. Date: Tue, 11 Jan 94 17:38:41 EDT
  13. Organization: Well, I haven't decided on a name yet...
  14. Lines: 150
  15.  
  16. In article <1E25B549H000493E2H@newton.han.de> NEWTON@MULTICOM.han.de writes:
  17. > my problem is how to get the WindowPtr of the actual CLI-Window where my
  18. > program is running in.
  19. >
  20. > It means:
  21. > I have the FileHandlePtr to the IO-File:
  22. >
  23. > 1: MyFileHandle := Open(ADR("CON:"), newFile);
  24. > or
  25. > 2: MyFileHandle := Output();
  26. >
  27. > and need the WindowPtr of the Window open bye 'CON:' if there is an
  28. > Outputwindow.
  29. >
  30. > My Language is Modula-II but if your example is in 'C', I can read it too.
  31.  
  32. This is an FAQ. Here is my solution:
  33.  
  34. /*
  35. **    ConWindow.c
  36. **
  37. **    Michael B. Smith
  38. **    February 2, 1993
  39. **
  40. **    No rights reserved. :)
  41. */
  42.  
  43. #include <exec/types.h>
  44. #include <exec/ports.h>
  45. #include <exec/memory.h>
  46.  
  47. #include <dos/dos.h>
  48. #include <dos/dosextens.h>
  49.  
  50. #include <clib/dos_protos.h>
  51. #include <clib/exec_protos.h>
  52.  
  53. #include <intuition/intuition.h>
  54.  
  55. struct MsgPort *
  56. WhichPort (BPTR file)
  57. {
  58.     /*
  59.     **  WhichPort
  60.     **
  61.     **    If file is passed and non-NULL, find the MsgPort of
  62.     **    its handler process. If file is NULL, then if I am
  63.     **    a process, return the MsgPort of my console handler.
  64.     **
  65.     **    See The AmigaDOS Manual, pp. 425 & 395, and
  66.     **    AmigaMail II-24.
  67.     */
  68.  
  69.     struct MsgPort
  70.         *rslt = NULL;
  71.  
  72.     if (file) {
  73.         rslt = ((struct FileHandle *) (file << 2))->fh_Type;
  74.     }
  75.     else {
  76.         struct Task *me = FindTask (NULL);
  77.  
  78.         if (me->tc_Node.ln_Type == NT_PROCESS)
  79.             rslt = ((struct Process *) me)->pr_ConsoleTask;
  80.     }
  81.  
  82.     return rslt;
  83. }
  84.  
  85. struct Window *
  86. WhichWindow (struct MsgPort *port)
  87. {
  88.     /*
  89.     **  WhichWindow
  90.     **
  91.     **    Send a ACTION_DISK_INFO to the port passed in. It must be
  92.     **    a message-port for a console handler. That will return a
  93.     **    pointer to a filled-in "struct InfoData" and the window
  94.     **    will be contained in the id_VolumeNode field.
  95.     **
  96.     **    Note that the window will be NULL for an AUTO or AUX console.
  97.     */
  98.  
  99.     struct InfoData
  100.         *info;
  101.     long
  102.         rslt = 0;
  103.  
  104.     /* info must be long-word aligned */
  105.     info = AllocVec (sizeof (struct InfoData), MEMF_PUBLIC);
  106.     if (!info)
  107.         return NULL;
  108.  
  109.     if (port) {
  110.         rslt = DoPkt1 (port, ACTION_DISK_INFO, (long) (info >> 2));
  111.  
  112.         if (rslt) {
  113.             rslt = info->id_VolumeNode;
  114.         }
  115.     }
  116.     FreeVec (info);
  117.  
  118.     return (struct Window *) rslt;
  119. }
  120.  
  121. int
  122. main (int argc, char **argv)
  123. {
  124.     struct MsgPort
  125.         *port;
  126.     struct Window
  127.         *mywin;
  128.  
  129.     /*
  130.     **  If we get an argument, use Input (), otherwise find the default...
  131.     */
  132.  
  133.     /*
  134.     **  You must be careful that Input() is from CON:. If not, results
  135.     **  are undefined in this context...
  136.     */
  137.     if (argc > 1) {
  138.         printf ("Window from Input()\n");
  139.         port = WhichPort (Input ());
  140.     }
  141.     else {
  142.         printf ("Window from ConsoleTask\n");
  143.         port = WhichPort (NULL);
  144.     }
  145.  
  146.     if (port) {
  147.         mywin = WhichWindow (port);
  148.         if (!mywin)
  149.             printf ("Couldn't find window\n");
  150.         else {
  151.             printf ("Left %ld, Top %ld,  Width %ld, Height %ld\n",
  152.                 mywin->LeftEdge, mywin->TopEdge,
  153.                 mywin->Width,     mywin->Height);
  154.         }
  155.     }
  156.     else {
  157.         printf ("Couldn't find port\n");
  158.     }
  159.  
  160.     exit (0);
  161. }
  162.  
  163. --
  164.   //   Michael B. Smith
  165. \X/    mbs@adastra.cvl.va.us  -or-  uunet.uu.net!virginia.edu!adastra!mbs
  166.